package com.supor.aiot.common.ui.scan.widget;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.SystemUtils;
import com.supor.aiot.common.R;
import com.supor.aiot.common.ui.scan.Utils;


/**
 * 负责绘制扫码窗口、边角及周边阴影
 */
public class FinderView extends View {

    private static final int DEFAULT_SHADOW_COLOR = 0x66000000;

    private int scanWindowLeft, scanWindowTop, scanWindowRight, scanWindowBottom;
    private Bitmap leftTopCorner, rightTopCorner, leftBottomCorner, rightBottomCorner;
    private int cornerWidth = 0;
    private int lineWidth = 1;
    private int lineOffset = 9;
    private Paint paint;
    private int shadowColor;

    public FinderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public FinderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        applyConfig(context, attrs);
        setVisibility(INVISIBLE);
        initCornerBitmap(context);

        paint = new Paint();
        paint.setAntiAlias(true);
        Logc.d("dpi: " + SystemUtils.getWidthDpi(context));
        float density = context.getResources().getDisplayMetrics().density;
        if (density < 2.3f) {
            lineOffset = 6;
        } else if (Math.abs(density - 2.5f) < 0.3f) {
            lineOffset = 7;
        } else if (Math.abs(density - 3f) < 0.3f) {
            lineOffset = 8;
        } else if (density > 3.3f) {
            lineOffset = 9;
        }
        Logc.d("density: " + context.getResources().getDisplayMetrics().density);
    }

    private void applyConfig(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.scan);
            shadowColor = typedArray.getColor(R.styleable.scan_shadowColor, DEFAULT_SHADOW_COLOR);
            typedArray.recycle();
        }
    }

    private void initCornerBitmap(Context context) {
        Resources res = context.getResources();
        leftTopCorner = BitmapFactory.decodeResource(res, R.drawable.scan_window_corner_left_top);
        rightTopCorner = BitmapFactory.decodeResource(res, R.drawable.scan_window_corner_right_top);
        leftBottomCorner = BitmapFactory.decodeResource(res,
                R.drawable.scan_window_corner_left_bottom);
        rightBottomCorner = BitmapFactory.decodeResource(res,
                R.drawable.scan_window_corner_right_bottom);
        cornerWidth = leftTopCorner.getWidth();
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        drawShadow(canvas);
        drawCorner(canvas);
        drawLine(canvas);
        drawLineShadow(canvas);
    }

    private void drawCorner(Canvas canvas) {
        paint.setAlpha(255);
        canvas.drawBitmap(leftTopCorner, scanWindowLeft, scanWindowTop, paint);
        canvas.drawBitmap(rightTopCorner, scanWindowRight - rightTopCorner.getWidth(),
                scanWindowTop, paint);
        canvas.drawBitmap(leftBottomCorner, scanWindowLeft,
                scanWindowBottom - leftBottomCorner.getHeight(), paint);
        canvas.drawBitmap(rightBottomCorner, scanWindowRight - rightBottomCorner.getWidth(),
                scanWindowBottom - rightBottomCorner.getHeight(), paint);
    }

    private void drawShadow(Canvas canvas) {
        paint.setColor(shadowColor);
        canvas.drawRect(0, 0, getWidth(), scanWindowTop, paint);
        canvas.drawRect(0, scanWindowTop, scanWindowLeft, scanWindowBottom, paint);
        canvas.drawRect(scanWindowRight, scanWindowTop, getWidth(), scanWindowBottom, paint);
        canvas.drawRect(0, scanWindowBottom, getWidth(), getHeight(), paint);
    }

    private void drawLine(Canvas canvas) {
        paint.setColor(0xffffffff);
        // 左边竖线
        canvas.drawRect(scanWindowLeft + lineOffset,
                scanWindowTop + cornerWidth,
                scanWindowLeft + lineWidth + lineOffset,
                scanWindowBottom - cornerWidth, paint);
        // 上面横线
        canvas.drawRect(scanWindowLeft + cornerWidth,
                scanWindowTop + lineOffset,
                scanWindowRight - cornerWidth,
                scanWindowTop + lineWidth + lineOffset, paint);
        // 下面横线
        canvas.drawRect(scanWindowLeft + cornerWidth,
                scanWindowBottom - lineWidth - lineOffset,
                scanWindowRight - cornerWidth,
                scanWindowBottom - lineOffset, paint);
        // 右边竖线
        canvas.drawRect(scanWindowRight - lineWidth - lineOffset,
                scanWindowTop + cornerWidth,
                scanWindowRight - lineOffset,
                scanWindowBottom - cornerWidth, paint);
    }

    private void drawLineShadow(Canvas canvas) {
        paint.setColor(shadowColor);
        // 左边竖线
        canvas.drawRect(scanWindowLeft,
                scanWindowTop + cornerWidth,
                scanWindowLeft + lineOffset,
                scanWindowBottom - cornerWidth, paint);
        // 上面横线
        canvas.drawRect(scanWindowLeft + cornerWidth,
                scanWindowTop,
                scanWindowRight - cornerWidth,
                scanWindowTop + lineOffset, paint);
        // 下面横线
        canvas.drawRect(scanWindowLeft + cornerWidth,
                scanWindowBottom - lineOffset,
                scanWindowRight - cornerWidth,
                scanWindowBottom, paint);
        // 右边竖线
        canvas.drawRect(scanWindowRight - lineOffset,
                scanWindowTop + cornerWidth,
                scanWindowRight,
                scanWindowBottom - cornerWidth, paint);
    }

    /**
     * 根据 RayView 的位置决定扫码窗口的位置
     */
    public void setScanWindowLocation(int left, int top, int right, int bottom) {
        scanWindowLeft = left - lineOffset;
        scanWindowTop = top - lineOffset;
        scanWindowRight = right + lineOffset;
        scanWindowBottom = bottom + lineOffset;
        invalidate();
        setVisibility(VISIBLE);
    }

    public void setShadowColor(int shadowColor) {
        this.shadowColor = shadowColor;
    }

    public void setCornerColor(int angleColor) {
        leftTopCorner = Utils.changeBitmapColor(leftTopCorner, angleColor);
        rightTopCorner = Utils.changeBitmapColor(rightTopCorner, angleColor);
        leftBottomCorner = Utils.changeBitmapColor(leftBottomCorner, angleColor);
        rightBottomCorner = Utils.changeBitmapColor(rightBottomCorner, angleColor);
    }
}
